<?php

namespace backend\modules\articlesite\controllers;

use backend\controllers\AdminController;
use common\components\Upload;
use common\models\articlesite\ArticlesiteCategory;
use Yii;
use common\models\articlesite\ArticlesiteArticle;
use backend\modules\articlesite\models\ArticlesiteArticleSearch;
use yii\helpers\Url;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * ArticleController implements the CRUD actions for ArticlesiteArticle model.
 */
class ArticleController extends AdminController
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    public function actions()
    {
        return [
            'upload' => [
                'class' => 'kucha\ueditor\UEditorAction',
                'config' => [
                    "imageUrlPrefix"  => Yii::getAlias('@uploadurl'),//图片访问路径前缀
                    "pathFormat" => "/images/{yyyy}/{mm}/{rand:7}" //上传保存路径
                ],
            ]
        ];
    }


    /**
     * Lists all ArticlesiteArticle models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new ArticlesiteArticleSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single ArticlesiteArticle model.
     * @param string $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new ArticlesiteArticle model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new ArticlesiteArticle();
        $cate = ArticlesiteCategory::findAll(['status'=>1]);
        if(!$cate){
            return $this->dialog('请先添加分类','warning','category/index');
        }
        if ($model->load(Yii::$app->request->post())) {
           if(false !== $photo = Upload::upload($model,'thumb')){
               $model->setAttribute('thumb',$photo);
           }
            if(false !== $file = Upload::upload($model,'file','2bin/doc')){
                $model->setAttribute('file',$file);
            }
           if($model->save()) {
               return $this->redirect(['view', 'id' => $model->id]);
           }
           return $this->dialog($this->checkError($model),'warning');
       } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing ArticlesiteArticle model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param string $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post())) {
            if(false !== $photo = Upload::upload($model,'thumb')){
                $model->setAttribute('thumb',$photo);
            }

            if(false !== $file = Upload::upload($model,'file','2bin/doc')){
                $model->setAttribute('file',$file);
            }
            $model->content = html_entity_decode($model->content);
            if($model->save()) {
                return $this->redirect(['view', 'id' => $model->id]);
            }
            return $this->dialog($this->checkError($model));
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing ArticlesiteArticle model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param string $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the ArticlesiteArticle model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param string $id
     * @return ArticlesiteArticle the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = ArticlesiteArticle::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
